//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.mvc.sub.facade
{
	import com.rogo.mvc.core.Controller;
	import com.rogo.mvc.core.Model;
	import com.rogo.mvc.core.View;
	import com.rogo.mvc.interfaces.IController;
	import com.rogo.mvc.interfaces.IFacade;
	import com.rogo.mvc.interfaces.IMediator;
	import com.rogo.mvc.interfaces.IModel;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.interfaces.IProxy;
	import com.rogo.mvc.interfaces.IView;
	import com.rogo.mvc.sub.observer.Notification;
	
	/**
	 * Facade 类是 View类 Controller类 Model类的经纪人， 为了简化程序开发，
	 * Facade已经完成了对View类， Controller类 Model类的单例的构造，
	 * 
	 * @author coco
	 */	
	public class Facade implements IFacade
	{
		private static var instance:IFacade;
		private var view:IView;
		private var controller:IController;
		private var model:IModel;
		
		public function Facade()
		{
			super();
			
			initController();
			initView();
			initModel();
		}
		
		public static function getInstance():IFacade
		{
			if (!instance) instance = new Facade();
			return instance;
		}
		
		protected function initController():void
		{
			controller = Controller.getInstance();
		}
		
		protected function initView():void
		{
			view = View.getInstance();
		}
		
		protected function initModel():void
		{
			model = Model.getInstance();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Command
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 注册命令
		 * 
		 * @param notificationName
		 * @param commandClassRef
		 */		
		public function registerCommand(notificationName:String, commandClassRef:Class):void
		{
			controller.registerCommand(notificationName, commandClassRef);
		}
		
		/**
		 * 移除命令
		 * 
		 * @param notificationName
		 */		
		public function removeCommand(notificationName:String):void
		{
			controller.removeCommand(notificationName);
		}
		
		/**
		 * 检索命令是否已经存在
		 * 
		 * @param notificationName
		 * @return 
		 */		
		public function hasCommand(notificationName:String):Boolean
		{
			return controller.hasCommand(notificationName);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Mediator
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 注册视图代理
		 * 
		 * @param mediator
		 */		
		public function registerMediator(mediator:IMediator):void
		{
			view.registerMediator(mediator);
		}
		
		/**
		 * 移除视图代理
		 * 
		 * @param mediatorName
		 */		
		public function removeMediator(mediatorName:String):void
		{
			view.removeMediator(mediatorName);
		}
		
		/**
		 * 检索视图代理是否已经存在
		 * 
		 * @param mediatorName
		 * @return 
		 */		
		public function hasMediator(mediatorName:String):Boolean
		{
			return view.hasMediator(mediatorName);
		}
		
		/**
		 * 通知观察者观察者
		 * 
		 * @param notification
		 */		
		public function notifyObservers(notification:INotification):void
		{
			view.notifyObservers(notification);
		}
		
		/**
		 * 发送通知
		 * 
		 * @param notificationName
		 * @param body
		 */		
		public function sendNotification(notificationName:String, param:Object=null):void
		{
			view.notifyObservers(new Notification(notificationName, param));
		}
		
		//--------------------------------------------------------------------------
		//
		//  Proxy
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 注册数据代理
		 * 
		 * @param proxy
		 */		
		public function registerProxy(proxy:IProxy):void
		{
			model.registerProxy(proxy);
		}
		
		/**
		 * 移除数据代理
		 * 
		 * @param proxyName
		 * @return 
		 */		
		public function removeProxy(proxyName:String):IProxy
		{
			return model.removeProxy(proxyName);
		}
		
		/**
		 * 检索数据代理
		 * 
		 * @param proxyName
		 * @return 
		 */		
		public function retrieveProxy(proxyName:String):IProxy
		{
			return model.retrieveProxy(proxyName);
		}
		
		/**
		 * 数据代理是否存在
		 * 
		 * @param proxyName
		 * @return 
		 */		
		public function hasProxy(proxyName:String):Boolean
		{
			return model.hasProxy(proxyName);
		}
		
	}
}